package com.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.info.GoodsInfo;
import com.info.UserInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sql.Sql_user;

public class UserAction extends ActionSupport{
	
	
	/**
	 * 序列化版本为1L
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 登录
	 * 
	 */
	public String login(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		if(username.equals("")||username==null||password.equals("")||password==null){
			System.out.println("用户名和密码不能为空");
			this.addActionError("用户名和密码不能为空");
			return ERROR;
		}else{
			Sql_user sqlUser=new Sql_user();
			UserInfo user=sqlUser.login(username);
			if(user==null){
				System.out.println("用户名不存在");
				this.addActionError("用户名不存在");
				return ERROR;
			}else{
				if(user.getPassword().equals(password)){
					System.out.println("登陆成功...");
					System.out.println("用户id号"+user.getId());
					System.out.println("********************"+user.getId()+"号用户开始服务********************");
					session.setAttribute("user", user);
					session.setAttribute("id", user.getId());
					session.setAttribute("isLogin", "login");
					return SUCCESS;
				}else{
					System.out.println("密码错误...");
					this.addActionError("密码错误");
					return ERROR;
				}	
			}
		}
		
	}
	
	/**
	 *注册
	 * 
	 */
	public String register(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String username=request.getParameter("username");
		String pas=request.getParameter("password");
		String repas=request.getParameter("repassword");
		String name=request.getParameter("name");
		String phone=request.getParameter("phone");
		String ques1=request.getParameter("question1");
		String ques2=request.getParameter("question2");
		String answer1=request.getParameter("answer1");
		String answer2=request.getParameter("answer2");
		if(!pas.equals(repas)){
			this.addActionError("密码不相同！");
			return ERROR;
		}
		if(username.equals("")||username==null||pas.equals("")||pas==null||
			repas.equals("")||repas==null||name.equals("")||name==null||
			phone.equals("")||phone==null||ques1.equals("")||ques1==null||
			ques2.equals("")||ques2==null||answer1.equals("")||answer1==null||
			answer2.equals("")||answer2==null){
			this.addActionError("信息不完整!");
			return ERROR;
		}
		UserInfo userInfo=new UserInfo();
		userInfo.setAnswer1(answer1);
		userInfo.setAnswer2(answer2);
		userInfo.setName(name);
		userInfo.setPassword(pas);
		userInfo.setPhone(phone);
		userInfo.setQuestion1(ques1);
		userInfo.setQuestion2(ques2);
		userInfo.setUsername(username);
		System.out.println("注册用户信息为:"+userInfo);
		Sql_user user=new Sql_user();
		UserInfo info=user.login(userInfo.getUsername());
		if(info!=null){
			System.out.println("用户名已存在!");
			this.addActionError("用户名已存在!");
			return ERROR;
		}else{
			if(user.register(userInfo).equals("success")){
				System.out.println("注册成功！");
				session.setAttribute("mainInfo", "恭喜注册成功");
				return SUCCESS;
			}else{
				this.addActionError("未知错误!");
				return ERROR;
			}
		}	
		
	}
	/**
	 * 退出登录
	 */
	
	public String unlogin(){
		HttpServletRequest request=ServletActionContext.getRequest();
		Map session=ActionContext.getContext().getSession();
		System.out.println(request.getSession().getAttribute("id")+"用户退出登录");
		System.out.println("********************"+request.getSession().getAttribute("id")+"号用户结束服务********************");
		session.clear();	
		return SUCCESS;
	}
	
	/**
	 * 忘记密码——检验账号
	 */
	
	public String forgetPas(){
		System.out.println("-------------------忘记密码-------------------");
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		Sql_user sqlUser=new Sql_user();
		String username=request.getParameter("username");
		if(username==null||username.equals("")){
			this.addActionError("账号不能为空");
			return ERROR;
		}
		UserInfo user=sqlUser.login(username);
		if(user==null){
			this.addActionError("没有此账号");
			System.out.println("没有账号:"+username);
			return ERROR;
		}else{
			session.setAttribute("user", user);
			System.out.println("查找到账号:"+username);
			return SUCCESS;
		}
	}
	
	/**
	 * 核对密保问题
	 */
	public String forAnswer(){
		System.out.println("-------------------密保验证-------------------");
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String answer1=request.getParameter("answer1");
		String answer2=request.getParameter("answer2");
		System.out.println("接收到数据: 答案1"+answer1+", 答案2"+answer2);
		UserInfo user=(UserInfo) session.getAttribute("user");
		if(answer1!=null&&!answer1.equals("")&&answer2!=null&&!answer2.equals("")){
			if(answer1.equals(user.getAnswer1())&&answer2.equals(user.getAnswer2())){
				System.out.println("密保正确");
				return SUCCESS;
			}else{
				this.addActionError("答案错误");
				System.out.println("答案错误");
				return ERROR;
			}
			
		}else{
			this.addActionError("信息不完整");
			System.out.println("信息不完整");
			return ERROR;
		}
	}
	
	/**
	 * 存入新密码
	 */
	
	public String newPas(){
		System.out.println("-------------------存入新密码-------------------");
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String pas=request.getParameter("password");
		String repas=request.getParameter("repassword");
		UserInfo user=(UserInfo) session.getAttribute("user");
		if(pas!=null&&!pas.equals("")&&repas!=null&&!repas.equals("")){
			if(pas.equals(repas)){
				Sql_user sql=new Sql_user();
				if(sql.saveUserInfo(user.getId(),"password", pas).equals("success")){
					System.out.println("存入成功!");
					session.setAttribute("mainInfo", "修改密码成功");
					session.removeAttribute("user");
					return SUCCESS;
				}else{
					System.out.println("未知错误");
					this.addActionError("未知错误");
					return ERROR;
				}
			}else{
				this.addActionError("密码不相同");
				System.out.println("密码不相同");
				return ERROR;
			}
		}else{
			this.addActionError("信息不完整");
			System.out.println("信息不完整");
			return ERROR;
		}
	}
	
	/**
	 * 购买商品
	 */
	
	public String buyGoods(){
		System.out.println("-------------------购买物品-------------------");
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String price1=request.getParameter("price");
		String id1=request.getParameter("id");
		double price=Double.valueOf(price1);
		int id=Integer.parseInt(id1);
		System.out.println("接收到数据"+price+","+id);
		UserInfo user=(UserInfo) session.getAttribute("user");
		if(user==null){
			System.out.println("用户未登陆");
			return "login";
		}else{
			if(user.getIntegral()>=price){
				user.setIntegral(user.getIntegral()-price);
				HashMap<Integer, GoodsInfo> goods=(HashMap<Integer, GoodsInfo>) session.getAttribute("allGoods");
				GoodsInfo goodsInfo=goods.get(id);
				goodsInfo.setStock(goodsInfo.getStock()-1);
				goodsInfo.setSales(goodsInfo.getSales()+1);
				Sql_user sql=new Sql_user();
				if(sql.buyGoods(user,goodsInfo).equals("success")){
					System.out.println(id+"号物品购买成功");
					session.setAttribute("mainInfo", "购买成功");
					return SUCCESS;
				}else{
					System.out.println("未知错误");
					session.setAttribute("mainInfo", "购买失败");
					return ERROR;
				}	
			}else{
				System.out.println("积分不足");
				session.setAttribute("mainInfo", "积分不足");
				return ERROR;
			}
		}
	}
	
	public void goodsInfo(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		String type1=request.getAttribute("type").toString();
		int type=Integer.parseInt(type1);
		System.out.println("获取到type"+type);
		Sql_user sql=new Sql_user();
		HashMap<Integer,GoodsInfo> allGoods=sql.goodsInfo(type);
		session.setAttribute("allGoods", allGoods);
		session.setAttribute("goodsNum", allGoods.size());
	}
}
